<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use TencentCloud\Common\Credential;
use TencentCloud\Common\Profile\ClientProfile;
use TencentCloud\Common\Profile\HttpProfile;
use TencentCloud\Common\Exception\TencentCloudSDKException;
use TencentCloud\Captcha\V20190722\CaptchaClient;
use TencentCloud\Captcha\V20190722\Models\DescribeCaptchaResultRequest;

class VerificatCodeController extends BaseController
{
    public function doValidate(Request $request)
    {
        $param = $request->input();
        if (empty($param['ticket'])) {
            return $this->ResultJson(0, '验证票据不能为空', []);
        }
        if (empty($param['userip'])) {
            return $this->ResultJson(0, '外网IP不能为空', []);
        }
        if (empty($param['randstr'])) {
            return $this->ResultJson(0, '随机字串不能为空', []);
        }
        try {
            // 实例化一个认证对象，入参需要传入腾讯云账户 SecretId 和 SecretKey，此处还需注意密钥对的保密
            // 代码泄露可能会导致 SecretId 和 SecretKey 泄露，并威胁账号下所有资源的安全性。以下代码示例仅供参考，建议采用更安全的方式来使用密钥，请参见：https://cloud.tencent.com/document/product/1278/85305
            // 密钥可前往官网控制台 https://console.cloud.tencent.com/cam/capi 进行获取
            $SecretId =config("omnipay.TengCloud.SecretId");//  "AKID2wjicKphXDh0NhW8fy1vgx8Ni9cylU4a";
            $SecretKey = config("omnipay.TengCloud.SecretKey");//"WowqVyCPVehkfQFzZ9Ypz1gQ6nWQOSM6";
            $cred = new Credential($SecretId, $SecretKey);
            // 实例化一个http选项，可选的，没有特殊需求可以跳过
            $httpProfile = new HttpProfile();
            $httpProfile->setEndpoint("captcha.tencentcloudapi.com");

            // 实例化一个client选项，可选的，没有特殊需求可以跳过
            $clientProfile = new ClientProfile();
            $clientProfile->setHttpProfile($httpProfile);
            // 实例化要请求产品的client对象,clientProfile是可选的
            $client = new CaptchaClient($cred, "", $clientProfile);
            // 实例化一个请求对象,每个接口都会对应一个request对象
            $req = new DescribeCaptchaResultRequest();
            $params = array(
                "CaptchaType"=>9,
                "Ticket"=>$param['ticket'],
                "UserIp"=>$param['userip'],
                "Randstr"=>$param['randstr'],
                "CaptchaAppId"=>config("omnipay.TengCloud.CaptchaAppId"),//196598172,
                "AppSecretKey"=>config("omnipay.TengCloud.AppSecretKey"),//"bfldgT0qBvKUH63t0wTK43cz7",
            );
            $req->fromJsonString(json_encode($params));
            // 返回的resp是一个DescribeCaptchaResultResponse的实例，与请求对象对应
            $resp = $client->DescribeCaptchaResult($req);
            // 输出json格式的字符串回包
            $dd = json_decode($resp->toJsonString(),true);
            if($dd['CaptchaCode'] ==1 && $dd['CaptchaMsg'] =="OK"){
                return $this->ResultJson(1, '验证成功', []);
            }else{
                return $this->ResultJson(0, '验证失败', []);
            }
        } catch (TencentCloudSDKException $e) {
            echo $e;
        }











        //存在文件且合法
        if ($request->hasFile('file') && $request->file('file')->isValid()) {
            //获取传递过来的文件
            $file = $request->file('file');
            //设置合法后缀名
            $allowed_extensions = ["png", "jpg", "jpeg", "gif", "doc", "docx", "txt", "pdf", "xls", "xlsx", "ppt", "pptx", "mp3", "mp4","wav"];
            //如果上传的文件后缀名不在定义数组
            if (!in_array($file->getClientOriginalExtension(), $allowed_extensions)) {
                return $this->ResultJson(0, '文件格式不正确!', []);
            } else {
                $destinationPath = 'upload/images/' . date("Y-m-d") . '/'; //public 文件夹下面建 uploads 文件夹
                //获取文件的后缀名
                $extension = $file->getClientOriginalExtension();
                //给上传文件定义文件名
                $fileName = md5(time() . rand(000000, 999999)) . '.' . $extension;
                //文件转换
                $file->move($destinationPath, $fileName);
                //设置文件路径
//                $filePath = asset($destinationPath . $fileName);
                $filePath = 'images/' . date("Y-m-d") . '/' . $fileName;
                return $this->ResultJson(1, '上传成功!', [$filePath]);
            }
        } else {
            return $this->ResultJson(0, '文件上传失败!', []);
        }
    }
}
